//author:jxx
//此处是对表单的方法，组件，权限操作按钮等进行任意扩展(方法扩展可参照SellOrder.js)
import gridFooter from "./product/Mall_good.vue"
import gridHeader from './product/Producttype.vue'
import AsyncLoading from "@/components/basic/AsyncLoading.vue";
import protype from './product/Producttype.vue'
import protypeAttr from './product/Product_Attr.vue'

let extension = {
  components: {//动态扩充组件或组件路径
    //表单header、content、footer对应位置扩充的组件
    protypeAttr: protypeAttr,
    gridHeader: gridHeader,//{ template: "<div>扩展组xx件</div>" },
    gridBody: "",
    gridFooter: gridFooter,
    //弹出框(修改、编辑、查看)header、content、footer对应位置扩充的组件
    modelHeader: protypeAttr,
    modelBody: () => ({ component: import("./product/Mall_GoodAndPackage.vue"), loading: AsyncLoading }),
    modelFooter: '',
    protype: protype
  },
  buttons: { view: [], box: [], detail: [] },//扩展的按钮

  methods: {//事件扩展
    getcolumn(val, name) {
      this.$refs.modelBody.clear();
      this.$refs.modelBody.getcolum(val, 'Add', this.editFormFileds.name, this.editFormFileds.code);
    },
    getFormOption(field) {
      let option;
      this.editFormOptions.forEach(x => {
        x.forEach(item => {
          if (item.field == field) {
            option = item;
          }
        })
      })
      return option;
    },

    // parentCall:(obj)=>
    // {
    //   this.goosat();
    // },
    order() {
      //this.$refs.gridHeader.reset();
      this.$refs.gridHeader.open1();
    },
    open1() {
      this.getinitDictionary();
      this.model.box2 = true;
    },
    modelOpenAfter() {
      //获取当前弹出框是新建还是编辑状态
      let isEDIT = this.currentAction == this.const.EDIT;
      var selectOption = this.getFormOption('labelName');
      //console.log(isEDIT)
      // if(!isEDIT) {
      //     selectOption.onInput = (val) => {

      //     this.editFormFields.labelCode=this.base.chineseToPinYin(val);
      //     }
      // }
      var Code = this.getFormOption('labelCode');
      this.$set(Code, "disabled", true)
    },
    getinitDictionary() {
      let distributor = this.getFormOption("distributor");
      let paytype = this.getFormOption("pay_type");
      this.http.post("api/Dictionary/GetVueDictionary", ["distributorList", "reservationType"], false).then(reslut => {
        let datas = [];
        reslut.find(item1 => item1.dicNo == "distributorList").data.forEach(item1 => {
          let items = {}
          items.value = item1.value;
          items.key = item1.key;
          datas.push(items)
        })
        distributor.data = datas;
        let data1s = [];
        reslut.find(item1 => item1.dicNo == "reservationType").data.forEach(item1 => {
          let items = {}
          items.value = item1.value;
          items.key = item1.key;
          data1s.push(items)
        })
        paytype.data = data1s;
      });
    },

    goosat() {
      let distributor = this.getFormOption("mainLabel");
      this.http.post("api/Dictionary/GetVueDictionary", ["Label"], false).then(reslut => {
        let datas = [];
        reslut.find(item1 => item1.dicNo == "Label").data.forEach(item1 => {
          // console.log(item1);
          let items = {}
          items.value = item1.value;
          items.key = item1.key;
          datas.push(items)
        })
        distributor.data = datas;
      });
    },

    onInit() {
      this.boxOptions.width = 900

      this.maxBtnLength = 5;
      this.boxOptions.height = document.documentElement.clientHeight * 0.8;
      var selectOption = this.getFormOption('mainLabel');
      this.pagination.sortName = "CreateDate";  //设置排序字段
      this.pagination.order = "desc"; //排序方式desc或者asc
      this.editFormOptions.forEach(x => {
        x.forEach(item => {
          // if(item.field == 'mainLabel'){
          //   item.extra = {
          //     icon :"ios-search",//显示图表
          //     text:"添加标签",//显示文本
          //     style:"color:red;",//指定样式
          //     click:item=>{  //触发事件
          //       this.$refs.gridHeader.open1();
          //       //this. goosat();
          //     //  this.showtype();
          //     }
          //   }
          // }
          if (item.field == 'type') {
            item.extra = {
              icon: "ios-search",//显示图表
              text: "选择属性",//显示文本
              style: "color:green;",//指定样式
              click: item => {  //触发事件

                if (this.editFormFileds.type.length > 0) {
                  this.$refs.modelHeader.open();
                } else {
                  this.$Message.error("请先选择产品类型");
                }
              }
            }
          }
        })

      })
      // //地址下拉框
      // this.editFormOptions.forEach(x=>{
      //   x.forEach(item=>{
      //     if(item.field == 'mainLabel'){
      //        item.onChange = $event => {
      //          this.goosat();
      //        }
      //     }
      //   })
      // })
      //编辑
      this.columns.push({
        title: '操作',
        fixed: 'right',
        width: 140,
        render: (h, { row, column, index }) => {
          return h(
            "div", { style: {} }, [
            h(
              "a", {
              props: {},
              style: {},
              on: {
                click: (e) => {
                  this.linkData(row);
                }
              }
            },
              "编辑"
            ),
          ])
        }
      })
      //缓存当前对象
      this.$store.getters.data().reportPrice = this;
      this.editFormFileds.type = [];
      var selectOption = this.getFormOption('type');
      selectOption.type = "cascader";
      selectOption.changeOnSelect = true
      //初始一个空的数据源(必须)
      selectOption.data = [];
      selectOption.onChange = (val, item) => {
        this.editFormFileds.type = val;
        let productname = "";
        let prodctcode = "";
        var spare5Option = this.getFormOption("spare5");
        var spare6Option = this.getFormOption("spare6");
        if (val[0] == "KeCheng") {
          this.$set(spare5Option, "hidden", false)
          this.$set(spare6Option, "hidden", false)
        } else {
          this.$set(spare5Option, "hidden", true)
          this.editFormFileds.spare5 = "";
          this.$set(spare6Option, "hidden", true)
          this.editFormFileds.spare6 = "";
        }
        for (let i = item.length - 1; i > 0; i--) {
          if (i == item.length - 1) { productname += item[i].label; prodctcode += item[i].value };
          if (i == 0) { productname += item[i].label; prodctcode += item[i].value };
        }
        if (this.editFormFileds.name == '') {
          this.editFormFileds.name = productname;
          this.editFormFileds.code = prodctcode;
        }
        // this.getcolumn(val);
        //this.$set(selectOption, "disabled", true);
      }
      var Package = this.getFormOption('isPackage');
      Package.onChange = (val, item) => {
        this.$refs.modelBody.isPackage(val);
      }
      var effectiveStartDate = this.getFormOption('effectiveStartDate');
      var effectiveEndDate = this.getFormOption('effectiveEndDate');
      effectiveStartDate.onChange = (val, item) => {
        effectiveEndDate.min = val;
      }
      effectiveEndDate.onChange = (val, item) => {
        effectiveStartDate.max = val;
      }
    },
    onInited() {
      //this.height = this.height-300;
      this.detailOptions.height = this.detailOptions.height - 200;
      //设置主表求字段，后台需要实现SummaryExpress方法
    },
    rowClick({ row, column, event }) { //查询界面table点击行选中当前行
      this.$refs.gridFooter.$refs.tableList.load({ value: row.id, sort: "CreateDate" })
      this.$refs.table.$refs.table.toggleRowSelection(row);
      if (this.$refs.table.getSelected().length > 1) {
        this.$refs.gridFooter.$refs.tableList.reset()
      }
      return true;
    },
    searchAfter(rows) {
      //页面加载或者刷新数据后直接显示第一行的明细
      if (rows.length) {
        // this.$nextTick(() => {
        this.$refs.gridFooter.$refs.tableList.load({ value: rows[0].id, sort: "CreateDate" })
        // })
      } else {
        //没有数据时，清空明细数据
        this.$refs.gridFooter.$refs.tableList.reset()
      }
      return true;
    },
    addBefore(formData) {//新建前验证
      this.setFormData(formData);
      return this.validContent(formData);
    },
    updateBefore(formData) { //修改前验证
      this.setFormData(formData);
      return this.validContent(formData);
    },
    setFormData(formData) { //新建或编辑时，将从表1、2的数据提交到后台
      //后台从对象里直接取extra的值
      let extra = {
        mall_good: this.$refs.modelBody.$refs.table1.rowData,//获取从表1的行数据
        table2List: this.$refs.modelBody.$refs.table2.rowData//获取从表2的行数据
      }
      formData.extra = JSON.stringify(extra);
    },
    validContent(formData) {
      return true;
    },
    modelOpenBefore() {
      var selectOption = this.getFormOption('type');
      selectOption.data = [];
      //从后台接口返回数据源,格式见iview组件result(建议在modelOpenBefore方法中设置数据源)
      this.http.post("api/producttypes/GetProtypeList").then(result => {
        result.result.forEach(item => {
          selectOption.data.push(item)
        })
      })
    },
    modelOpenAfter() {
      this.boxOptions.height = document.documentElement.clientHeight * 0.8;
      let isEDIT = this.currentAction == this.const.EDIT;
      var typeOption = this.getFormOption("type");
      var typenameOption = this.getFormOption("typename");
      if (!isEDIT) {
        this.$set(typeOption, "hidden", false)
      } else {
        this.$set(typeOption, "hidden", true)
        // this.editFormFileds.type=[];
      }
      if (isEDIT) {
        this.$set(typenameOption, "hidden", false)

      } else {
        this.$set(typenameOption, "hidden", true)
        this.editFormFileds.typename = [];
      }
      //点击弹出框后，如果是编辑状态，禁止编辑帐号，如果新建状态，将帐号字段设置为可编辑
      this.editFormOptions.forEach(item => {
        item.forEach(x => {
          //如果是编辑帐号设置为只读
          // if (x.field == "code") {
          //     //disabled是editFormOptions的动态属性，这里只能通过this.$set修改值
          //     this.$set(x, "disabled", true)
          // }
          if (x.field == "type") {
            //disabled是editFormOptions的动态属性，这里只能通过this.$set修改值
            this.$set(x, "disabled", isEDIT)
            if (isEDIT) {
              x.extra = {}
            }

          }
          if (x.field == "typename") {
            this.$set(x, "disabled", isEDIT)
            if (isEDIT) {
              x.extra = {}
            }
          }
          if (x.field == "name") {
            //disabled是editFormOptions的动态属性，这里只能通过this.$set修改值
            if(!isEDIT) {
            x.onInput = (val) => {
              this.editFormFileds.code = this.base.chineseToPinYin(val)
            }
            }

          }

        })
      })
      if (this.$refs.modelBody) {
        //新建功时，清空 清空,从表1，从表2的数据
        if (this.currentAction == 'Add') {
          this.$refs.modelBody.resetRow();
          this.$refs.modelBody.$refs.table1.reset();
          this.$refs.modelBody.$refs.table2.reset();
        } else {
          this.$refs.modelBody.resetRow();
          //编辑时，加载从表1，从表2的数据
          this.$refs.modelBody.$refs.table1.load();
          this.$refs.modelBody.$refs.table2.load();
        }
      }
      //获取当前弹出框是新建还是编辑状态
    },
  }
};
export default extension;
